package com.qzdsoft.erpcloud.controller.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.domain.basic.BillDeocdeParam;
import com.qzdsoft.erpcloud.domain.basic.BillDeocdeType;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.basic.BillDeocdeParamService;
import com.qzdsoft.erpcloud.service.basic.BillDeocdeTypeService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.query.basic.BillDeocdeParamInfo;
import com.qzdsoft.erpcloud.vo.query.basic.BillDeocdeParamQueryParam;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.basic.BasicDecodeMethodEnum;
import com.qzdsoft.vo.basic.BasicDecodePrifixEnum;

/**
 * Created by Admin
 */
@Controller
@RequestMapping("/basic/bill/deocde/param")
public class BillDeocdeParamController {

//    @Autowired
//    private RedisHelper redisHelper;

    @Autowired
    private BillDeocdeParamService basicBillDeocdeParamService;

    @Autowired
    private BillDeocdeTypeService basicBillDeocdeTypeService;

    @Autowired
    private CenterCompanyService actCenterCompanyService;

    /**
     *
     * @param request
     * @param model
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model, @RequestParam("typeId") Integer typeId) {
        List<EnumPo> decodeMethodList = new ArrayList<EnumPo>();
        Map<Object, Object> decodeMethodMap = new HashMap<Object, Object>();
        for(BasicDecodeMethodEnum e : BasicDecodeMethodEnum.values()){
            SysUtils.enums(decodeMethodList, decodeMethodMap, e.getKey(), e.getVal());
        }

        List<EnumPo> decodePrifixList = new ArrayList<EnumPo>();
        Map<Object, Object> decodePrifixMap = new HashMap<Object, Object>();
        for(BasicDecodePrifixEnum e : BasicDecodePrifixEnum.values()){
            SysUtils.enums(decodePrifixList, decodePrifixMap, e.getKey(), e.getVal());
        }

        model.addAttribute("decodeMethodList", decodeMethodList);
        model.addAttribute("decodeMethodMap", decodeMethodMap);

        model.addAttribute("decodePrifixList", decodePrifixList);
        model.addAttribute("decodePrifixMap", decodePrifixMap);



        model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());

        model.addAttribute("type_id", typeId);

        BillDeocdeType basicBillDeocdeType = basicBillDeocdeTypeService.selectById(typeId);
        model.addAttribute("typeName", basicBillDeocdeType.getName());
        model.addAttribute("actCenterCompany", actCenterCompanyService.getById(basicBillDeocdeType.getCompany_id()));

        return "basic/bill_deocde/param_index";
    }

    /**
     *
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/edit")
    public ModelAndView edit(HttpServletRequest request, @RequestParam("id")  Integer id) {
        ModelAndView mv = new ModelAndView();
        
        List<EnumPo> decodeMethodList = new ArrayList<EnumPo>();
        Map<Object, Object> decodeMethodMap = new HashMap<Object, Object>();
        for(BasicDecodeMethodEnum e : BasicDecodeMethodEnum.values()){
            SysUtils.enums(decodeMethodList, decodeMethodMap, e.getKey(), e.getVal());
        }

        List<EnumPo> decodePrifixList = new ArrayList<EnumPo>();
        Map<Object, Object> decodePrifixMap = new HashMap<Object, Object>();
        for(BasicDecodePrifixEnum e : BasicDecodePrifixEnum.values()){
            SysUtils.enums(decodePrifixList, decodePrifixMap, e.getKey(), e.getVal());
        }
        
        mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
        mv.addObject("decodeMethodList", decodeMethodList);
        mv.addObject("decodeMethodMap", decodeMethodMap);

        mv.addObject("decodePrifixList", decodePrifixList);
        mv.addObject("decodePrifixMap", decodePrifixMap);
        
        if(null != id){
            BillDeocdeParam billDeocdeParam = basicBillDeocdeParamService.selectById(id);
            mv.addObject("billDeocdeParam", billDeocdeParam);
            mv.setViewName("/basic/bill_deocde/param_edit");
        }else {
            mv.setViewName("/basic/bill_deocde/param_add");
        }
        return mv;
    }

    /**
     * 执行编辑
     *
     * @param request
     * @param basicBillDeocdeParam
     * @return
     */
    @ResponseBody
    @SysLog(description="编辑单据编码保存")
    @RequestMapping("/edit_execute")
    public Object editExecute(HttpServletRequest request, BillDeocdeParam basicBillDeocdeParam) {
//        basicBillDeocdeParam.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicBillDeocdeParam.getCompany_id()));
        return basicBillDeocdeParamService.saveOrUpd(basicBillDeocdeParam);
    }
    /**
     * 执行新增
     *
     * @param request
     * @param basicBillDeocdeParam
     * @return
     */
    @ResponseBody
    @SysLog(description="新增单据编码保存")
    @RequestMapping("/create_execute")
    public Object createExecute(HttpServletRequest request, BillDeocdeParam basicBillDeocdeParam) {
//        basicBillDeocdeParam.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicBillDeocdeParam.getCompany_id()));
        return basicBillDeocdeParamService.saveOrUpd(basicBillDeocdeParam);
    }

    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="删除单据编码")
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return basicBillDeocdeParamService.del(id);
    }

    /**
     *
     * @param request
     * @param param
     * @return
     */
    @ResponseBody
    @SysLog(description="查询单据编码")
    @RequestMapping("/data")
    public LayTableResponse<BillDeocdeParamInfo> data(HttpServletRequest request, BillDeocdeParamQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
        Integer companyId = param.getCompanyId();
        if(null != companyId){
            param.setCompanyId(companyId);
        }
       return basicBillDeocdeParamService.selectData(param.getTypeId(), param.getCompanyId(), param.getDecodeMethod(), param.getPrifixFirst(), param.getPrifixSecond(), param.getPrifixThree(), param.getPage(), param.getLimit());
    }

}
